package com.ClasesGraficas;

import java.util.List;

import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.SimpleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYMultipleSeriesRenderer.Orientation;

import android.graphics.Color;
import android.graphics.Paint.Align;

public class VentasN2v2 {
	//### VARIABLES ###
	
	//Varibles del activity
		
	private XYMultipleSeriesDataset dataset = null;
	private XYMultipleSeriesRenderer mRenderer = null;
	private String[] meses = {"Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio",
	    					      "Agosto", "Septiembre", "Noviembre", "Diciembre"};
		
	//Variables que son pasadas al activity
	private List<double[]> eventos = null;
	private String[] titulos = null;
	private int[] colors = null;
	private String titulo;
	private int tipo;
	private List<String> lablesX = null;
	
	//Pintar reporte (tipo es: 1= anual, 2= trimestrual/mensual)
	public void ejecutar(List<double[]> eventos, String[] titulos, int tipo, List<String> labelsX) {			
		this.titulos = titulos;
		this.eventos = eventos;
		this.lablesX = labelsX;
		this.tipo = tipo;
		
		int [] coloresBasicos = {Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),
								Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0), Color.argb(255,90,250,210),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),
								Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0), Color.argb(255,90,250,210),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),
								Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0), Color.argb(255,90,250,210),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),
								Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0), Color.argb(255,90,250,210),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),
								Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0), Color.argb(255,90,250,210),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),
								Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0), Color.argb(255,90,250,210),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),
								Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0), Color.argb(255,90,250,210),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10),
								Color.argb(255,200,200,200), Color.YELLOW,	Color.argb(255,200,10,10), Color.argb(255,10,200,10), Color.argb(255,250,100,0),
								Color.argb(255,50,110,30), Color.argb(255,90,50,0), Color.argb(255,90,250,210),Color.argb(255,90,250,210), Color.argb(255,90,50,220),
								Color.argb(255,200,200,200), Color.YELLOW,Color.argb(255,250,100,0), Color.argb(255,200,10,10), Color.argb(255,10,200,10)};
				
		colors = new int[eventos.size()];
		for (int i=0;i<colors.length;i++)
			colors[i] = coloresBasicos[i];
		    
		mRenderer = buildBarRenderer(colors);
		mRenderer.setOrientation(Orientation.HORIZONTAL);
		
		String labelX = "";
		if(tipo==3)
			labelX = "Años";
		else if(tipo ==1)
			labelX = "Meses";
		else if(tipo == 2)
			labelX = "Trimestre";
		
	    setChartSettings(mRenderer, this.titulo, labelX, "Ventas", Color.GRAY, Color.LTGRAY);
	    
	    dataset = buildBarDataset(titulos, this.eventos);
	}
	
	public void setTituloChart(String titulo){
		this.titulo = titulo;
	}

	protected void setChartSettings(XYMultipleSeriesRenderer renderer, String title, String xTitle,
		      String yTitle, int axesColor,
		      int labelsColor) 
	{
		renderer.setXAxisMin(0);
		renderer.setXAxisMax(eventos.get(0).length + 1);
		renderer.setYAxisMin(0);
		
		double max =getMaxSale();
		renderer.setYAxisMax(max+ (getIndicador(max)/2));
		
		renderer.setChartTitle(title);
		renderer.setXTitle(xTitle);
		renderer.setYTitle(yTitle);
		renderer.setAxesColor(axesColor);
		renderer.setLabelsColor(labelsColor);
		renderer.setMargins(new int[] {40, 35, 35, 5}); //{arriba, izquierda, abajo, derecha}
		renderer.setBarSpacing(1);
		renderer.setShowGrid(true);
		renderer.setGridColor(Color.WHITE);
		renderer.setXLabels(eventos.get(0).length + 2); //Cantidad de lineas
		renderer.setYLabels(10); //Cantidad de lineas
		//renderer.setZoomRate(1.1f);
		//renderer.setZoomButtonsVisible(true);
		renderer.setClickEnabled(true);
		renderer.setPanEnabled(true,true);

		for(int i=0;i<this.lablesX.size();i++)
			renderer.addXTextLabel(i+1,this.lablesX.get(i));
		
	}
	
	protected XYMultipleSeriesRenderer buildBarRenderer(int[] colors) 
	{
		XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
	    renderer.setAxisTitleTextSize(30);
	    renderer.setChartTitleTextSize(30);
	    renderer.setLabelsTextSize(15);
	    renderer.setLegendTextSize(25);
	    renderer.setLegendHeight(75);
	    
	    int length = colors.length;
	    for (int i = 0; i < length; i++)
	    {
	    	SimpleSeriesRenderer r = new SimpleSeriesRenderer();
	    	r.setColor(colors[i]);
	    	r.setChartValuesSpacing(2);
	    	r.setDisplayChartValues(true);
	    	r.setChartValuesTextAlign(Align.LEFT);
	    	r.setChartValuesTextSize(24);
	    	renderer.addSeriesRenderer(r);
	    }
	    
	    return renderer;
	  }
	
	protected XYMultipleSeriesDataset buildBarDataset(String[] titles, List<double[]> values) 
	{
	    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
	    int length = titles.length;
	    
	    for (int i = 0; i < length; i++)
	    {
	    	CategorySeries series = new CategorySeries(titles[i]);
	    	double[] v = values.get(i);
	    	int seriesLength = v.length;
	    	
	    	for (int k = 0; k < seriesLength; k++) 
	    	{
	    		series.add(v[k]);
	    	}
	    	dataset.addSeries(series.toXYSeries());
	    }
	    
	    return dataset;
	  }

	public XYMultipleSeriesDataset getDataset() {
		return dataset;
	}

	public void setDataset(XYMultipleSeriesDataset dataset) {
		this.dataset = dataset;
	}

	public XYMultipleSeriesRenderer getmRenderer() {
		return mRenderer;
	}

	public void setmRenderer(XYMultipleSeriesRenderer mRenderer) {
		this.mRenderer = mRenderer;
	}
	
	public double getMaxSale()
	{
		double max = 0;
		
		for(int i=0;i<eventos.size();i++)
		{
			double[] aux = eventos.get(i);
			for(int j=0;j<aux.length;j++)
				if(max<aux[j])
					max = aux[j];
		}
			
		return max;
	}
	
	public int getIndicador(double max)
	{
		int indicador = 1;
		
		while (max >= 1)
		{
			max /= 10;
			if (max>=1)
			indicador *= 10;
		}
		return indicador;
		
	}
}